package com.knowledge.controller;

import com.knowledge.entity.Article;
import com.knowledge.service.ArticleService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @PostMapping
    public ResponseEntity<Article> createArticle(
            @RequestBody CreateArticleRequest request,
            @AuthenticationPrincipal UserDetails userDetails) {
        Article article = new Article();
        article.setTitle(request.getTitle());
        article.setContent(request.getContent());
        article.setSummary(request.getSummary());
        
        // 假设UserDetails的username是用户的ID
        Long authorId = Long.parseLong(userDetails.getUsername());
        Article createdArticle = articleService.createArticle(article, authorId);
        return ResponseEntity.ok(createdArticle);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Article> updateArticle(
            @PathVariable Long id,
            @RequestBody UpdateArticleRequest request) {
        Article article = new Article();
        article.setTitle(request.getTitle());
        article.setContent(request.getContent());
        article.setSummary(request.getSummary());
        
        Article updatedArticle = articleService.updateArticle(id, article);
        return ResponseEntity.ok(updatedArticle);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteArticle(@PathVariable Long id) {
        articleService.deleteArticle(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<Article> getArticle(@PathVariable Long id) {
        Article article = articleService.getArticle(id);
        articleService.incrementViewCount(id);
        return ResponseEntity.ok(article);
    }

    @GetMapping
    public ResponseEntity<Page<Article>> getAllArticles(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageRequest pageRequest = PageRequest.of(page, size, Sort.by("createdAt").descending());
        return ResponseEntity.ok(articleService.getAllArticles(pageRequest));
    }

    @GetMapping("/search")
    public ResponseEntity<Page<Article>> searchArticles(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        return ResponseEntity.ok(articleService.searchArticles(keyword, pageRequest));
    }

    @GetMapping("/hot")
    public ResponseEntity<Page<Article>> getHotArticles(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        return ResponseEntity.ok(articleService.getHotArticles(pageRequest));
    }

    @PostMapping("/{id}/like")
    public ResponseEntity<Article> likeArticle(@PathVariable Long id) {
        Article article = articleService.incrementLikeCount(id);
        return ResponseEntity.ok(article);
    }
}

@Data
class CreateArticleRequest {
    private String title;
    private String content;
    private String summary;
}

@Data
class UpdateArticleRequest {
    private String title;
    private String content;
    private String summary;
} 